package cn.com.api.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiU_message;

import cn.com.api.service.ApiU_messageService;
import cn.com.api.service.CheckService;

import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "message/")
public class ApiU_messageController {
	@Autowired
	private ApiU_messageService apiU_messageService;
	@Autowired
	private CheckService checkService;

	private static Logger logger = Logger.getLogger(ApiU_messageController.class);

	/**
	 * 获取消息中心--好友请求列表
	 * 
	 * @param request
	 * @param response
	 *            Type类型：0系统消息， 1好友请求， 2热门头条， 3好友动态，4 红包提醒，5商家对话， 6物流通知， 7交易消息
	 *            8申请加入圈子 9提醒发货
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getMessageListbyType")
	public void getMessageListbyType(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String type, PageInfo page) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (checkService.checkToken(user_id, token, map)) {
				// List<ApiU_message> message_list =
				// this.apiU_messageService.findU_messagesByMap(params, page);
				List<ApiU_message> message_list = this.apiU_messageService.findU_messages(user_id, type, page);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功！");
				map.put(Utils.DATAS, message_list);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 根据id查询
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "readMessage")
	public void readMessage(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String message_id) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (checkService.checkToken(user_id, token, map)) {
				map.clear();
				ApiU_message u_message = apiU_messageService.readMessage(message_id);
				map.put("datas", u_message);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "查询成功！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

}
